<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.List"%>    
<%@ page import="minipost.post.model.Post"%>
<%@ page import="minipost.post.PostBean"%>

<%
	request.setCharacterEncoding("UTF-8");

	Object user = session.getAttribute("user");
	if (user == null) {
		response.sendRedirect("login.jsp");
	}

	String title = request.getParameter("title");
	String author = request.getParameter("from");
	String type = request.getParameter("category");
	String content = request.getParameter("input-content");
	
	if (!(title == null || author == null || type == null || content == null)) {
		Post post = new Post(title, type, author, content);
		PostBean postBean = new PostBean();
		postBean.insert(post);
	}
%>

<%
                    PostBean postBean = new PostBean();
                    List<Post> list = postBean.getAllPosts();
                    int length = list.size();
                %>
<%
            int count = Integer.parseInt(postBean.getCount());
            String currentPageStr = request.getParameter("page");
            int     currentPage = 0;
            if (null == currentPageStr) {
                currentPage = 1;
            } else {
                currentPage = Integer.parseInt(currentPageStr);
            }
            int totalPage = (int)Math.ceil(1.0 * count/10);
            %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>管理</title>
    <link rel="stylesheet" type="text/css" href="css/admin.css"/>
    <style type="text/css"></style>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript">
    function delPost(obj, id) {
    	if (confirm("确认删除?")) {
    		$.ajax({
    			url: "/minipost/adminServlet?type=del&id=" + id,
    			type: 'get',
    			complete: function() {
    				$(obj).parent().parent().remove();		
    			}
    		});
    		
    	}
    }
    function edit(id) {
    	window.location.href = "edit.jsp?id=" + id;
    }
    </script>
</head>
<body>
    <div id="head">
        <P>管理</P>
    </div>
    <div id="nav">
        <ul>
            <li><a href="index.jsp">首页</a></li>
            <li><a href="publish.jsp">发表文章</a></li>
            <li><a href="management.jsp">文章管理</a></li>
            <li><a href="xgmm.jsp">修改密码</a></li>
            <li><a href="login.jsp">退出管理</a></li>
        </ul>
    </div>
    <div id="mid">
        <div class="content">
            <table>
                <thead>
                    <tr>
	                   <th width="5%">编号</th>
	                   <th width="65%">标题</th>
	                   <th width="15%">时间</th>
	                   <th width="15%">操作</th>
                    </tr>
                </thead>

                <tbody>
                <% for (int i = 0; i < length; ++i) { 
                		Post currentPost = list.get(i);
                %>
                    <tr>
                        <td><%= currentPost.getId()%></td>
                        <td><a href="single.jsp?id=<%= currentPost.getId()%>"><%= currentPost.getPostTitle()%></a></td>
                        <td><%= currentPost.getPostDateAsString()%></td>
                        <td><button onclick="edit(<%= currentPost.getId()%>)">修改</button><button class="btn-err" onclick="delPost(this, <%= currentPost.getId()%>)">删除</button></td>
                    </tr>
                <% } %>
                </tbody>
            </table>
            <div id="page">
            
                <form method="get" action="management.jsp">
                    <input type="hidden" name="cat" value="&lt;?php echo get_query_var('cat');?&gt;">
                    <span class="page_total">
                        第<%=currentPage%>页&nbsp;共<%=totalPage%>页&nbsp;<%=count%>条
                    </span>
                    <span class="page_item">
                        <a href="#&gt;" title="首页">首页</a>
                    </span>
                    <span class="page_item">
                        <a href="management.jsp?page=<%=totalPage %>" title="末页">末页</a>
                    </span>
                    <span class="page_item">
                        <a href="management.jsp?page=<%=(currentPage > 1 ? currentPage - 1 : "javascript:void(0)")%>" title="">上一页</a>
                    </span>
                    <span class="page_item">
                        <a href="#" title="management.jsp?page=<%=(currentPage >= totalPage ? "javascript:void(0)" : currentPage + 1)%>">下一页</a>
                    </span>
                    <span class="page_item">
                        <a href="javascript:void(0);" title="">跳转到</a>
                    </span>
                    
                    <span class="page_item">
                        <input type="text" name="page" id="jump_page">
                    </span>
                    <span class="page_item">
                        <input type="submit" value="跳转" id="jump_submit">
                    </span>
                </form>
            </div>
        </div>
    </div>
    <div id="foot">
        Copyright&copy;&nbsp;合肥工业大学网站设计基础
    </div>
</body>
</html>